
var initData = function(){
	var url = base+"/projectCenter/businessdata/mentionDataManage";
	var param = {mention_data_id:mention_id,project_id:project_id};
	$.ajax({
        url: url,
        data: param,
        success: function (data, textStatus, jqXHR) {
        	var html = data.form;
        	$("#view").html("");
        	$("#view").html(html);
        	upload();
        },
        error: function () {
        }
    });
}



$("body").undelegate(".layui-upload-file","click").delegate(".layui-upload-file","click",function(){
	var data_id = $(this).attr("data_id");
	var file_type = $(this).attr("file_type");
	upload(data_id,file_type);
}); 



$("body").undelegate("[preview_path]","click").delegate("[preview_path]","click",function(){
    var preview_path = $(this).attr("preview_path");
    if(preview_path == "" || preview_path == undefined || preview_path == null){
        layer.msg('文件不存在,无法预览!', {icon: 2,time:2000});
        return false;
    }else{
        var url = base+"/projectCenter/sjps/previewDataFile/"+preview_path;
        $.ajax({
            url: url,
            async:false,
            success: function (data, textStatus, jqXHR) {
                $("iframe[data_id='"+preview_path+"']").attr("src",data);
            },
            error: function () {
            }
        });
        return false;
    }
});



$("body").undelegate("[download_path]","click").delegate("[download_path]","click",function(){
	var download_path = $(this).attr("download_path");
	if(download_path == "" || download_path == undefined || download_path == null){
		layer.msg('文件不存在,无法下载!', {icon: 2,time:2000});
		return false;
	}else{
		var show_file_url = base+"/datacenter/common/getFileById?file_id="+download_path;
		window.open(show_file_url,"_self")
		return false;
	}
});


$("div").undelegate().delegate("#submit_btn","click",function(){
 	//提交保存
 	var data_arr = $('form').serializeArray();
 	var data_json = {};
 	$.each(data_arr,function(index,value){
 		var field_name = value.name;
 		var field_value = value.value;
 		data_json[field_name] = field_value;
 	});
	$.post(base+"/projectCenter/businessdata/updateMentionDataByTable/"+mention_id,data_json,
		function (data,status){
		if(data!=""&&data!=null){
			if(data.statusCode == 0){
				layer.msg('更新成功', {icon: 1,time:2000});
			}else{
				layer.msg(data.message, {icon: 2,time:2000});
			}
		}else{
			layer.msg('更新失败!', {icon: 2,time:2000});
		}
		　　setTimeout(initData,2000);
		});
	return false;
 });



var upload = function(data_id , file_type){
	var allow_format;
	if("image"==file_type){
		allow_format = 'jpg|png|gif';
	}else if("excel"==file_type){
		allow_format = 'xls|xlsx';
	}else if("word" == file_type){
		allow_format = 'doc|docx';
	}else if("pdf" == file_type){
		allow_format = 'pdf';
	}else if("dwg" == file_type){
		allow_format = 'dwg';
	}
	layui.upload({
		  url: base+'/projectCenter/uploadFileApi/uploadFile/'+project_id
		  ,type:'file'
		  ,ext:allow_format
		  ,success: function(res){
			  if(res.code == '0'){
				  var original_file_name = res.original_file_name;
				  var file_id = res.file_id;
				  $("input[name='"+data_id+"']").val(file_id);
				  $("[download_path]").attr("download_path",file_id);
				  $("[preview_path]").attr("preview_path",file_id);
                 // $("iframe[data_id]").attr("data_id",data_id);
                  if(res.src != ""){
                      $("iframe[data_id='"+data_id+"']").attr("src",res.src);
                  }
				  layer.msg(res.msg, {icon: 1,time:2000});
			  }else{
				  layer.msg(res.msg, {icon: 2,time:2000});
			  }
		  }
	});
}

$(document).ready(function(){
	initData();
});